package com.ztstkj.lzscale.adpters

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.sscl.baselibrary.utils.BigDecimalUtils
import com.ztstkj.lzscale.R
import com.ztstkj.lzservices.model.SettlementEnum

class CartAdapter(private var datas: MutableList<CartItem>) : RecyclerView.Adapter<CartAdapter.ItemViewHolder>() {
    lateinit var context: Context
    private var selectedPoisiton: Int = RecyclerView.NO_POSITION
    var onCartItemDel:ICartItemDelClickListener?=null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        context = parent.context
        val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.activity_cart_item, parent, false)
        return ItemViewHolder(v)
    }

    @SuppressLint("UseCompatLoadingForDrawables", "NotifyDataSetChanged", "SetTextI18n")
    override fun onBindViewHolder(holder: ItemViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val bean = datas[position]
        holder.imgCartIcon.setImageDrawable(context.getDrawable(context.resources.getIdentifier(bean.productImage,"drawable",context.packageName)) )
//        val resId = context.resources.getIdentifier(bean.productImage, "drawable", context.packageName)
//        val cartIcon = context.resources.getDrawable(resId, null)
        //holder.imgCartIcon.setImageDrawable(cartIcon)
        holder.tvCartName.text = bean.productName
        holder.tvCartWeight.text=bean.productWeight.toString()+"g"
        if(bean.settlement== SettlementEnum.POINT.value){
            holder.tvCartPrice.text= bean.productPoint.toString()+"积分/kg"
            holder.tvCartAmount.text= bean.productPointAmount.toString()+"积分"
        }else if(bean.settlement== SettlementEnum.CASH.value){
            holder.tvCartPrice.text= BigDecimalUtils.round(bean.productPrice.toString(),2)+"元/kg"
            holder.tvCartAmount.text= BigDecimalUtils.round(bean.prodcutPriceAmount.toString(),2)+"元"
        }
        //holder.onSelect(position == selectedPoisiton)
        holder.clCartItem.setOnClickListener {
            selectedPoisiton = position
            notifyDataSetChanged()
            //itemClickListener?.onItemClickListener(position)
        }
        holder.clCartItemDel.setOnClickListener{
            datas.removeAt(position)
            notifyDataSetChanged()
            onCartItemDel!!.onClick()
        }
    }


    override fun getItemCount(): Int {
        return datas.size
    }

    inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imgCartIcon: ImageView = view.findViewById(R.id.imgCartIcon)
        val tvCartName: TextView = view.findViewById(R.id.tvCartName)
        val tvCartPrice: TextView = view.findViewById(R.id.tvCartPrice)
        val tvCartAmount: TextView = view.findViewById(R.id.tvCartAmount)
        val tvCartWeight: TextView = view.findViewById(R.id.tvCartWeight)
        val clCartItem: ConstraintLayout = view.findViewById(R.id.clCartItem)
        var clCartItemDel:ConstraintLayout= view.findViewById(R.id.clCartItemDel)

        @SuppressLint("UseCompatLoadingForDrawables")
        fun onSelect(isSelected: Boolean) {
            if (isSelected) {
                clCartItem.background= context.getDrawable(R.drawable.recyle_cart_item_bg)
            } else {
                clCartItem.background= context.getDrawable(R.color.white)
            }
        }
    }
    //自定义接口
    interface ICartItemDelClickListener {
        fun onClick()
    }
}